package common

import (
	"fmt"
	"sale-admin/internal/define"
	"strconv"
	"time"

	"github.com/golang-module/carbon"
)

// 处理起止时间 年月日时分秒
func HandlerTime(startTime, endTime *string, DateType int) {
	if DateType == define.HOUR {
		if *startTime != "" && *endTime != "" {
			*startTime = carbon.Parse(*startTime).ToDateTimeString()
			*endTime = carbon.Parse(*endTime).EndOfDay().ToDateTimeString()
		} else { //今天
			*startTime = carbon.Now().StartOfDay().ToDateTimeString()
			*endTime = carbon.Now().EndOfDay().ToDateTimeString()
		}
	} else if DateType == define.DAY {
		if *startTime != "" && *endTime != "" {
			*startTime = carbon.Parse(*startTime).ToDateTimeString()
			*endTime = carbon.Parse(*endTime).EndOfDay().ToDateTimeString()
		} else { //最近两周
			*startTime = carbon.Now().SubDays(13).StartOfDay().ToDateTimeString()
			*endTime = carbon.Now().ToDateTimeString()
		}
	} else if DateType == define.WEEK {
		if *startTime != "" && *endTime != "" {
			*startTime = carbon.Parse(*startTime).ToDateTimeString()
			*endTime = carbon.Parse(*endTime).EndOfDay().ToDateTimeString()
		} else { //最近一月
			*startTime = carbon.Now().SubDays(30).StartOfDay().ToDateTimeString()
			*endTime = carbon.Now().ToDateTimeString()
		}
	} else if DateType == define.MONTH {
		if *startTime != "" && *endTime != "" {
			*startTime = carbon.Parse(*startTime).StartOfMonth().ToDateTimeString()
			if carbon.Parse(*endTime).Layout("200601") == carbon.Parse("now").Layout("200601") { //当月
				*endTime = carbon.Now().ToDateTimeString()
			} else {
				*endTime = carbon.Parse(*endTime).EndOfMonth().ToDateTimeString()
			}
		} else { //默认今年
			*startTime = carbon.Now().StartOfYear().ToDateTimeString()
			*endTime = carbon.Now().ToDateTimeString()
		}
	} else if DateType == define.YEAR {
		if *startTime != "" && *endTime != "" {
			*startTime = carbon.Parse(*startTime).StartOfYear().ToDateTimeString()
			*endTime = carbon.Parse(*endTime).EndOfYear().ToDateTimeString()
		} else { //默认今年
			*startTime = carbon.Now().StartOfYear().ToDateTimeString()
			*endTime = carbon.Now().ToDateTimeString()
		}
	}
}

func GetPeriodsByType(startTime, endTime string, DayType int) (FullTimeList []string) {
	if DayType == define.HOUR {
		return GetHourPeriods(startTime, endTime)
	} else if DayType == define.DAY {
		return GetDatePeriods(startTime, endTime)
	} else if DayType == define.WEEK {
		return GetDatePeriods(startTime, endTime)
	} else if DayType == define.MONTH {
		return GetMonthPeriods(startTime, endTime)
	} else if DayType == define.YEAR {
		return GetYearPeriods(startTime, endTime)
	}
	return
}

// 2006-01-02 15:00
func GetHourPeriods(startTime, endTime string) (FullTimeList []string) {
	start := carbon.Parse(startTime).ToStdTime()
	end := carbon.Parse(endTime).ToStdTime()
	for start.Before(end) || start.Equal(end) {
		nowString := start.Format("2006-01-02 15:00")
		FullTimeList = append(FullTimeList, nowString)
		start = start.Add(1 * time.Hour)
	}
	return
}

// 2006-01-02
func GetDatePeriods(startTime, endTime string) (FullTimeList []string) {
	start := carbon.Parse(startTime).ToStdTime()
	end := carbon.Parse(endTime).ToStdTime()
	for start.Before(end) || start.Equal(end) {
		FullTimeList = append(FullTimeList, start.Format("2006-01-02"))
		start = start.AddDate(0, 0, 1)
	}
	return
}

// 2006-01
func GetMonthPeriods(startTime, endTime string) (FullTimeList []string) {
	start := carbon.Parse(startTime).ToStdTime()
	end := carbon.Parse(endTime).ToStdTime()
	for start.Before(end) || start.Equal(end) {
		FullTimeList = append(FullTimeList, start.Format("2006-01"))
		start = start.AddDate(0, 1, 0)
	}
	return
}

// 2006
func GetYearPeriods(startTime, endTime string) (FullTimeList []string) {
	start := carbon.Parse(startTime).ToStdTime()
	end := carbon.Parse(endTime).ToStdTime()
	for start.Before(end) || start.Equal(end) {
		MonthString := start.Format("2006")
		FullTimeList = append(FullTimeList, MonthString)
		start = start.AddDate(1, 0, 0)
	}
	return
}

// 获取一年中的第几周【2022_01】，修正年初的年份
func GetWeekOfYear(date string) string {
	year := carbon.Parse(date).Year()
	month := carbon.Parse(date).Month()
	weekOfYear := carbon.Parse(date).WeekOfYear()
	if month == 1 && weekOfYear >= 52 {
		year = year - 1
	}
	return fmt.Sprintf("%d_%02d", year, weekOfYear)
}

// 获取一年中的第几周【202201】，修正年初的年份
func GetWeekOfYearInt(date string) int {
	year := carbon.Parse(date).Year()
	month := carbon.Parse(date).Month()
	weekOfYear := carbon.Parse(date).WeekOfYear()
	if month == 1 && weekOfYear >= 52 {
		year = year - 1
	}
	yearWeek, _ := strconv.Atoi(fmt.Sprintf("%d%02d", year, weekOfYear))
	return yearWeek
}
